function log(result, error) {
  if (!error) {
    $("debug").update(JSON.stringify(result) + "\n");
  } else {
    $("debug").update("ERROR <pre>" + JSON.stringify(result).replace(/(\\n)+/g, "<br/>") + "</pre>\n");
  }
}

//Event.observe($('signup-form'), 'submit', validate_and_post, false);
function formCallback(result, form) {
  window.status = "valiation callback for form '" + form.id + "': result = " + result;
}
Validation.addAllThese([
    ['validate-password', 'Your password must be more than 6 characters and not be \'password\' or the same as your name', {
      minLength : 7,
      notOneOf : ['password','PASSWORD','1234567','0123456'],
      notEqualToField : 'email'
    }],
    ['validate-password-confirm', 'Your confirmation password does not match your first password, please try again.', {
      equalToField : 'password'
    }],
    ['validate-username', 'Your username must be at least 6 characters long and must be alphanumeric.', {
      minLength : 7,
      pattern : /^[a-zA-Z\d]+$/
    }],

    ]);
function erlang_create_accont(form, onComplete, onError) {
  var accountService = new JsonRpcTransaction("/json_rpc/account_json_service", "signup", form.serialize({hash: true,submit: 'delete' }));
  accountService.addCallback(onComplete);
  accountService.addErrorCallback(onError);
}
function create_test_offline_account(form, onComplete, onError) {
  $('progress').show();
  onComplete({success: true, message: "Account succesfullt created"});
  //onComplete({success: true, message: "Account succesfullt created"});
  //onError({success: true, message: "Account succesfullt created"});

  var r = function(){
    onComplete({success: true, message: "Account succesfullt created"});
    $('progress').hide();
    //simulation of redirect
    document.location.href='../tabularasa/tabularasa.html';
  };
  r(5);

}
function create_accont(form, onComplete, onError) {
  if (offline) {
    create_test_offline_account(form, onComplete, onError);
  } else {
    erlang_create_accont(form, onComplete, onError);
  }
}
function validate_and_post(valid) {
  //$('submit_button').disable();
  var vrs = valid.validate();
  valid.decorateAll(vrs);
  //console.log(Object.toJSON(vrs));
  if (vrs.pluck('results').flatten().pluck('passes').all()) {
    function onComplete(result) {
      if(result[0] == "validation_error" && !result[1].pluck('results').flatten().pluck('passes').all()) {
        valid.decorateAll(result[1]);
      } else {
        document.location.href='../tabularasa/tabularasa.html';
      }
    }
    function onError(result) {
      log(result);
    }
    create_accont($('signup-form'), onComplete, onError);
  } else {
    return false;
  }
}
Ajax.Responders.register({
  onCreate: function() {
    $('progress').show();
  },
  onComplete: function() {
    if (0 == Ajax.activeRequestCount)
      $('progress').hide();
  }
});
var offline = /^file:\/\//.test(document.location);

function bindUI() {
  var valid = new Validation('signup-form', {immediate : true, onSubmit : false});
  $("email").focus();
  $('signup-form').observe('submit', function(e) {
    e.stop();
    validate_and_post(valid);
    return false;
  });
}
document.observe('dom:loaded', bindUI);

